package rise.ui;

import rise.comunity.Community;
import rise.net.ReceiverService;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class UserListActivity extends RiseActivity implements OnItemClickListener {

	private UserAdapter userAdapter;
	private ListView userListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_list);
		UserListActivity.userListActivity = this;
		
		userListView = (ListView) findViewById(R.id.list);
		userListView.setOnItemClickListener(this);
		userAdapter = new UserAdapter(this, R.id.list);
		userListView.setAdapter(userAdapter);
	}

	@Override
	protected void onResume() {
		super.onResume();

		UserListActivity.refreshUserListActivity();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	
		UserListActivity.refreshUserListActivity();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//Log.e("-","Destroy UserList");
	}

	private static UserListActivity userListActivity;
	public static UserListRefreshHandler handler;

	public synchronized static void refreshUserListActivity() {
		if (userListActivity == null)
			return;
		Community c = Community.obtainCommunity();
		userListActivity.userAdapter.clear();
		userListActivity.userAdapter.addAll(c.getUserList());
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		String username = userAdapter.getItem(position);
		Intent intent = new Intent(this, Conversation.class);

		intent.putExtra("username", username);
		startActivity(intent);
	}

}
